C++ Pointers and Arrays

03-11-17 Course- CPP

Pointers are the variables that hold address. Pointers can point at cells of an array not only single variable. Consider this example:


int* ptr;
int a[5];
ptr = &a[2];  // &a[2] is the address of third element of a[5].

Pointer pointing to array cell

Suppose, pointer needs to point to the fourth element of an array, that is, hold address of fourth array element in above case. Since ptr points to the third element in the above example, ptr + 1 points to the fourth element. You may think that, ptr + 1 may hold the address of byte next to ptr. But it's not correct. It is because pointer ptr is a pointer to int and size of int is fixed for a operating system (size of int is 4 byte of 64-bit operating system). Hence, the address between ptr and ptr + 1 differs by 4 bytes. If pointer ptr was pointer to char then, the address between ptrand ptr + 1 would have differed by 1 byte since size of a character is 1 byte.

Example 1: C++ Pointers and Arrays

C++ Program to display address of elements of an array using both array and pointers


#include <iostream>
using namespace std;

int main() {
    float a[5];
    float *ptr;
    
    cout << "Displaying address using arrays: "<<endl;
    for (int i = 0; i < 5; ++i) {
        cout << "&a[" << i << "] = " << &a[i] <<endl;
    }

    ptr = a;   // ptr = &a[0]
    cout<<"\nDisplaying address using pointers: "<< endl;
    for (int i = 0; i < 5; ++i) {
        cout << "ptr + " << i << " = "<<ptr+i <<endl;
    }

    return 0;
}

Output


Displaying address using arrays: 
&a[0] = 0x7fff5fbff880
&a[1] = 0x7fff5fbff884
&a[2] = 0x7fff5fbff888
&a[3] = 0x7fff5fbff88c
&a[4] = 0x7fff5fbff890

Displaying address using pointers: 
ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890

In the above program, different pointer is used for displaying the address of array elements. But, array elements can be accessed using pointer notation (by using same array name). For example:


int p[3];

&p[0] is equivalent to p
&p[1] is equivalent to p+1
&p[2] is equivalen to p+2

Example 2: Pointer and Arrays

C++ Program to display address of array elements using pointer notation.


#include <iostream>
using namespace std;

int main() {
    float a[5];
    
    cout<<"Displaying address using pointers notation: "<< endl;
    for (int i = 0; i < 5; ++i) {
        cout << a+i <<endl;
    }

    return 0;
}

Output


Displaying address using pointers notation: 
0x7fff5fbff8a0
0x7fff5fbff8a4
0x7fff5fbff8a8
0x7fff5fbff8ac
0x7fff5fbff8b0

You know that, pointer ptr holds the address and expression *ptr gives the content in that address. Similarly, *(ptr + 1) gives the content in address ptr + 1. Consider this code below:


int p[5] = {3, 4, 5, 5, 3};
  • &p[0] is equal to p and *p is equal to p[0]
  • &p[1] is equal to p+1 and *(p+1) is equal to p[1]
  • &p[2] is equal to p+2 and *(p+2) is equal to p[2]
  • &p[i] is equal to p+i and *(p+i) is equal to p[i]

Example 3: C++ Pointer and Array

C++ Program to insert and display data entered by using pointer notation.


#include <iostream>
using namespace std;

int main() {
    float a[5];
    
// Inserting data using pointer notation
    cout << "Enter 5 numbers: ";
    for (int i = 0; i < 5; ++i) {
        cin >> *(a+i) ;
    }

    
// Displaying data using pointer notation
    cout << "Displaying data: " << endl;
    for (int i = 0; i < 5; ++i) {
        cout << *(a+i) << endl ;
    }

    return 0;
}

Output


Enter 5 numbers: 2.5
3.5
4.5
5
2
Displaying data: 
2.5
3.5
4.5
5
2